package org.geektimes.projects.user.web.controller;

import org.geektimes.projects.user.domain.User;
import org.geektimes.projects.user.service.UserService;
import org.geektimes.projects.user.service.UserServiceImpl;
import org.geektimes.web.mvc.controller.PageController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.POST;
import javax.ws.rs.Path;

/**
 * 注册
 *
 * @author 邵小飒
 * @version 1.0
 * @date 2021/3/1 下午10:44
 */
@Path("/api")
public class RegisterController implements PageController {


    @POST
    @Path("/register") // /hello/world -> HelloWorldController
    public String execute(HttpServletRequest request, HttpServletResponse response) throws Throwable {
        UserService userService = new UserServiceImpl();
        User user = new User();
        user.setEmail(request.getParameter("email"));
        user.setName(request.getParameter("username"));
        if (!(request.getParameter("pwd2").equals(request.getParameter("pwd")))) {
            return "error.jsp";
        }
        user.setPassword(request.getParameter("pwd"));
        user.setPhoneNumber(request.getParameter("mobile"));
        userService.register(user);
        return "success.jsp";
    }
}
